const db = require('../db/db')
const {paging} = require('../utils/paging')
module.exports = {
    // 获取分类
    getCategorizes: async (ctx,next)=>{
        const pageObj = await paging(ctx)
        const startPage = pageObj.startPage
        const endPage = pageObj.endPage
        await db.promise().query('select * from categorizes where isdel=0 ').then(async res=>{
            const total =  res[0].length
            await db.promise().query('select * from categorizes where isdel=0  order by create_time desc limit ?,?',[startPage,endPage]).then(ress=>{
                ctx.body = {
                    code:1000,
                    msg:'获取成功',
                    categorizes:ress[0],
                    total:total
                }
            })
        })
        
    },
    // 添加分类
    addCategorize: async (ctx,next)=>{
        const {categorize_name, author} = ctx.request.body
        const create_time = new Date()
        const isExit =  await db.promise().query('select categorize_name from categorizes where categorize_name=?',[categorize_name])
        if(isExit[0].length >= 1){
            ctx.body = {
                code:202,
                msg:'该分类已存在'
            }
        }else{
            await db.promise().query('insert into categorizes(categorize_name,author,create_time) values(?,?,?)',
            [categorize_name, author, create_time]
            ).then(res=>{
                if(res[0].affectedRows == 1){
                    ctx.body = {
                        code:200,
                        msg:'创建成功'
                    }
                }
            }).catch(err=>{
                ctx.body = {
                    code:201,
                    msg:'创建失败'+err
                }
            })
        }
    },
    // 删除和恢复分类
    delcategorize:async (ctx, next)=> {
        const {isdel, categorize_id} = ctx.request.body
        const isdelVal = isdel==0 ? 1:0
        await db.promise().query('update categorizes set isdel=? where id=?',[isdelVal, categorize_id]).then(res=>{
            if(res[0].affectedRows == 1){
                if(isdelVal == 1){
                    ctx.body = {
                        code:200,
                        msg:'删除成功'
                    }
                }else{
                    ctx.body = {
                        code:200,
                        msg:'恢复成功'
                    }
                }
            }
        }).catch(err=>{
            ctx.body = {
                code:201,
                msg:'删除失败'+err
            }
        })
    },
    // 修改分类
    editCategorize: async (ctx, next)=> {
        const {categorize_id,categorize_name} = ctx.request.body
        const isExit =  await db.promise().query('select categorize_name from categorizes where categorize_name=?',[categorize_name])
        if(isExit[0].length >= 1){
            ctx.body = {
                code:202,
                msg:'该分类已存在'
            }
        }else{
            await db.promise().query('update categorizes set categorize_name=? where id=?',[categorize_name,categorize_id]).then(res=>{
                if(res[0].affectedRows == 1){
                    ctx.body = {
                        code:200,
                        msg:'修改成功'
                    }
                }
            }).catch(err=>{
                ctx.body = {
                    code:201,
                    msg:'修改失败'
                }
            })
        }
    },
}
 